

function equipmentInfoCtrl($scope, $rootScope, $http, ngDialog, paFetchService) {
  'ngInject';
  if ($state.params.orgId) {
    this.defaultOrgId = $state.params.orgId;
  }

  let deviceLevel = $state.params.deviceLevel; // 设备等级
  let inspectionRecords = $state.params.inspectionRecords; // 设备持证

  this.queryData = {
    deviceLevel: deviceLevel,
    inOutStatus: deviceLevel != undefined || inspectionRecords != undefined ? '1' : undefined,
    inspectionRecords: inspectionRecords,
    orgId: this.defaultOrgId,
  };

  if (this.defaultOrgId) {
    this.queryData.inOutStatus = 1;
  }

  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  }

  // 页码变化时调用方法
  $scope.pageChange = () => {
    $http.post(`${SERVER_CONTEXT}/api/deviceinfo/queryDeviceList`,
      angular.extend(this.queryData, this.paginationData)
        )
      .then((resp) => {
        this.equipmentItemList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };

  // 分页查询方法
  this.paginationQuery = () => {
    $http.post(`${SERVER_CONTEXT}/api/deviceinfo/queryDeviceList`,
      angular.extend(this.queryData, this.paginationData))
      .then((resp) => {
        this.equipmentItemList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };


  // 查询按钮
  this.query = () => {
    this.paginationQuery();
  };

  //  初始页面
  this.initPage = () => {
    if (this.defaultOrgId) {
      this.paginationQuery();
    }
  }

  // 全选toggle按钮
  this.toggleAll = () => {
    this.equipmentItemList.forEach((item) => {
      item.checked = this.toggleAllFlag;
    });
  }

  // 判断全选人
  this.isChange = () => {
    var bool = true;
    this.equipmentItemList.forEach((item) => {
      if(!item.checked){
        bool = false;
      }
    });
    this.toggleAllFlag = bool;
  }

  // 查看
  this.goNewPage = (id) => {
    let url = $state.href('equipmentInfoDetail',{id: id});
    window.open(url, '_blank');
  }

  // 出场登记
  this.openEquipPatchExitDialog = () => {
    let selectedEquipList = [], shouldContinue = true, hasCheckedItem = false;

    // 获得选中的设备
    let length = this.equipmentItemList.length;
    for (let i = 0; i < length; i++) {
      let item = this.equipmentItemList[i];
      if (item.checked) {
        hasCheckedItem = true;
        shouldContinue = item.exitTime ? false : true;
        selectedEquipList.push(item);
      }
      if (!shouldContinue) {
        layer.alert('选择项内有已出场设备,请重新选择');
        return false;
      }
    }

    if (!selectedEquipList.length) {
      layer.alert('请选择出场设备');
      return false;
    }

    $scope.selectedEquipList = selectedEquipList;
    ngDialog.open({
      template: 'equipPatchExitDialog',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme pa-confirm-theme',
      controllerAs: 'vm',
      scope: $scope,
      controller: function($http, $scope) {
        'ngInject';
        this.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }

          let outPlantTime = this.outPlantTime;
          $scope.selectedEquipList.forEach((item) => {
            item.outPlantTime = outPlantTime;
          });
          let reqData = {deviceInOutRecordBean: $scope.selectedEquipList};
          $http({
            method: 'POST',
            url: `${SERVER_CONTEXT}/api/deviceinfo/updateDeviceInOutRecord`,
            data: reqData,
          }).then((resp) => {
            if (resp.data.success) {
              $scope.equipmentInfo.paginationQuery();
              layer.msg(resp.data.message);
              $scope.closeThisDialog();
            } else {
              $scope.closeThisDialog();
              layer.msg('服务器错误,请联系技术人员');
            }
          });
        }
      },
    });
  }

  let getSelectedEquips = () => {
    let selectedEquipList = [];
    let length = this.equipmentItemList.length;
    for (let i = 0; i < length; i++) {
      let item = this.equipmentItemList[i];
      if (item.checked) {
        selectedEquipList.push(item);
      }
    }
    return selectedEquipList;
  }

  // 打印二维码
  this.printQrCode = () => {
    let selectedEquipList = getSelectedEquips();
    if (!selectedEquipList.length) {
      layer.alert('请选择设备');
      return;
    }
    $scope.selectedEquipList = selectedEquipList;
    ngDialog.openConfirm({
      template: 'equipInfoQrCodePrintDialog',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
      controllerAs: 'vm',
      controller: function($scope) {
        'ngInject';

        this.print = () => {
          $('.qr-print').print({ globalStyles: false, timeout: 1200 });
        }

        this.getQrCodeSrc = (item) => {
            let qrContent = encodeURIComponent(`#/equipmentInfo/${item.id}`);
            let requestUrlStr =
            `${$scope.serverContext}/api/qr?size=120&code=${qrContent}`;
            return requestUrlStr;
        }
      },
    });
  }

  // 删除设备按钮
  this.delete = (id) => {
    $scope.modalTitle = '删除设备信息';
    $scope.modalContent = '确认要删除该设备吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    })
      .then(() => {
        $http.delete(`${SERVER_CONTEXT}/api/deviceinfo/deleteDeivceInfo/${id}`)
          .then((resp) => {
            if (resp.statusText == 'OK') {
              layer.msg('删除成功');
              this.paginationQuery();
            }
          });
      });
  }

  // 重置按钮
  this.reset = () => {
    this.queryData = {};
    this.queryData.orgId = $rootScope.activeBid.orgId;
  };

  // 升序、降序
  this.up = (param,index) => {
    this.queryData.columeName = param;
    this.queryData.order = index;
    this.paginationQuery();
  }

  this.initPage();

}

export default equipmentInfoCtrl;